package cn.apthink.controller.system;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Inject;
import org.noear.solon.annotation.Mapping;

import cn.apthink.controller.BaseResult;
import cn.apthink.controller.DataResult;
import cn.apthink.controller.ListResult;
import cn.apthink.model.ADepartment;
import cn.apthink.model.FDepartment;
import cn.apthink.model.UDepartment;
import cn.apthink.mybatisplus.entity.DepartmentEntity;
import cn.apthink.mybatisplus.service.DepartmentService;
import cn.apthink.solon.Logging;
import cn.apthink.utility.TreeNode;
import cn.apthink.utility.ObjectUtil;
import cn.apthink.utility.Util;
import cn.dev33.satoken.annotation.SaCheckPermission;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

/**
 * 单位
 *
 * @author David唐
 * @createdate 2024年2月23日
 */
@Logging
@Slf4j
@Controller
@Api(value="单位控制器")
@Mapping("/api/department")
public class DepartmentController {
	@Inject
	DepartmentService departmentService;

	@Mapping("/delete")
	@ApiOperation("删除指定单位信息")
	@SaCheckPermission("department#delete")
	public BaseResult delete(@ApiParam(name = "id", value = "单位id", required = true) String id) {
		log.debug(id.toString());
		DepartmentEntity department = departmentService.getById(id);
		if (Util.isEmpty(department)) {
			return DataResult.error(99, "单位id不存在", "departmentid");
		}
		departmentService.removeById(id);
		return  DataResult.success("删除单位成功",department);
	}

	@Mapping("/add")
	@ApiOperation("新增单位信息")
	@SaCheckPermission("department#add")
	public BaseResult add(@ApiParam(name = "ADepartment", value = "单位信息", required = true) ADepartment data) {
		log.debug(data.toString());
		if (Objects.isNull(data.getDepartmentname())) {
			return DataResult.error(101, "单位名不能为空", "departmentname");
		}
		DepartmentEntity department = new DepartmentEntity();
		ObjectUtil.copy(data, department);
//		David: 采用MyBatisPlus自动填充功能
//		department.setId(Utils.guid());
//		department.setCreatetime(DateUtil.now());
		departmentService.save(department);
		return DataResult.success("新增单位成功",department);
	}

	@Mapping("/update")
	@ApiOperation("修改单位信息")
	@SaCheckPermission("department#update")
	public BaseResult update(@ApiParam(name = "UDepartment", value = "单位信息", required = true) UDepartment data) {
		log.debug(data.toString());
		if (Objects.isNull(data.getId())) {
			return DataResult.error(101, "id不能为空", "id");
		}
		if (Objects.isNull(data.getDepartmentname())) {
			return DataResult.error(101, "单位名不能为空", "departmentname");
		}
		DepartmentEntity department = departmentService.getById(data.getId());
		if (Objects.isNull(department)) {
			return DataResult.error(99, "单位id不存在", "departmentid");
		}
		ObjectUtil.copy(data, department);
//		David: 采用MyBatisPlus自动填充功能
//		department.setUpdatetime(DateUtil.now());
		departmentService.updateById(department);
		return DataResult.success("变更单位成功", department);
	}

	@Mapping("/query")
	@ApiOperation("查询单位信息")
	@SaCheckPermission("department#query")
	public ListResult<DepartmentEntity> query(@ApiParam(name = "FDepartment", value = "单位查询对象", required = true) FDepartment data) {
		log.debug(data.toString());
		List<DepartmentEntity> ds = departmentService.select(data.getDepartmentname(), data.getDepartmenttype(), data.getScheduletype());
		return ListResult.success(ds);
	}
	
	@Mapping("/all")
	@ApiOperation("所有单位信息")
	@SaCheckPermission("department#query")
	public ListResult<DepartmentEntity> all() {
		log.debug("all");
		List<DepartmentEntity> ds = departmentService.list();
		return ListResult.success(ds);
	}

	@Mapping("/simple")
	@ApiOperation("所有单位简明信息")
	@SaCheckPermission("department#query")
	public ListResult<TreeNode> simple() {
		log.debug("simple");
		List<DepartmentEntity> ds = departmentService.list();
		List<TreeNode> result = new ArrayList<TreeNode>();
		ds.forEach(x->{
			TreeNode node = new TreeNode();
			node.setName(x.getDepartmentname());
			node.setPid(x.getPid());
			node.setId(x.getId());
			result.add(node);
		});
		return ListResult.success(result);
	}
}